using System.Collections.Generic;
using Belmost4PDA.DataRows;
using Belmost4PDA.Fasade;

namespace Belmost4PDA.Logic
{
    public class DataProcesser
    {
        public enum Action
        {
            Select, GetGrid,
            GetGridRow, Update, Delete,
            Insert
        }

        public AbstractQueryBuilder queryBuilder = null;
        public DataProcesser(Action type)
        {
            switch (type)
            {
                case Action.Select:
                case Action.GetGridRow:
                    queryBuilder = new QuerySelectBuilder();
                    break;
                case Action.GetGrid:
                    queryBuilder = new QueryClsBuilder("not defined");
                    break;
                case Action.Update:
                    queryBuilder = new QueryUpdateBuilder();
                    break;
                case Action.Delete:
                    queryBuilder = new QueryDeleteBuilder();
                    break;
                case Action.Insert:
                    queryBuilder = new QueryInsertBuilder();
                    break;
            }
        }

        public void Select(List<AbstractMapper> AllMappers)
        {
            if (InterfaceDispatcher._instance.RunningState.StateId != Constants.ModesIDs.Mode_View)
            {
                queryBuilder.Create(AllMappers, GlobalStates.IDBR);
            }
            else
            {
                ((QuerySelectBuilder)queryBuilder).sql = string.Format(InterfaceDispatcher._instance.CurrentState.WorkedParam.TableName, GlobalStates.IDBR);
            }
            queryBuilder.Execute();
            DataRows.DataRows row = queryBuilder.Select();
            foreach (AbstractMapper mapper in AllMappers)
            {
                mapper.ToControls(row);
            }
            //throw new NullReferenceException();
        }
        /// <summary>
        /// select data into grid
        /// </summary>
        /// <param name="grid"></param>
        /// <param name="idMapper"></param>
        /// <returns>value of first id field</returns>
        public virtual string Select(Control_Grid grid, Mapper_IdMapper idMapper)
        {
            // get all items
            List<Control_GridItem> items = grid.GetColumns();
            List<AbstractMapper> gridMappers = new List<AbstractMapper>();
            gridMappers.Add(idMapper);
            foreach (Control_GridItem item in items)
            {
                gridMappers.Add(item.GetMapper());                
            }
            QuerySelectBuilder selBuilder = new QuerySelectBuilder();
            selBuilder.Create(gridMappers, GlobalStates.IDBR);

            ((QueryClsBuilder)queryBuilder).SQL = selBuilder.sql + ((grid.orderBy == "") ? ("") : (" " + grid.orderBy));
            queryBuilder.Execute();
            grid.ClearLines();
            DataRows.DataRows first = null;
            while(!((QueryClsBuilder)queryBuilder).Eof())
            {
                if(first == null)
                {
                    first = queryBuilder.Select();
                    grid.AddLine(first);
                }
                else 
                    grid.AddLine(queryBuilder.Select());
            }
            if (first == null) 
                return "";
            else
                return first.FieldByName(grid.GetIdFieldName());
        }
        public /*virtual*/ bool Update(string Table, string IdName, string IdValue, List<AbstractMapper> AllMappers)
        {
            List<AbstractMapper> copy= new List<AbstractMapper>();
            Mapper_IdMapper idmap=new Mapper_IdMapper(IdName,IdValue,Table);
            copy.Add(idmap);
            copy.AddRange(AllMappers);
            queryBuilder.Create(copy, GlobalStates.IDBR);
            queryBuilder.Execute();
            /// if(((QueryUpdateBuilder)(queryBuilder)).Done) return true;
            /// else return false;
            return ((QueryUpdateBuilder)queryBuilder).Done;
        }
        public virtual bool Delete(string Table, string IdName, string IdValue)
        {
            List<AbstractMapper> copy = new List<AbstractMapper>();
            Mapper_IdMapper idmap = new Mapper_IdMapper(IdName, IdValue, Table);
            copy.Add(idmap);
            queryBuilder.Create(copy, GlobalStates.IDBR);
            queryBuilder.Execute();
            return ((QueryDeleteBuilder) queryBuilder).Done;
        }
        public void SetEmpty(List<AbstractMapper> AllMappers)
        {
            
            foreach (AbstractMapper mapper in AllMappers)
            {
                DataRows.DataRows EmptyRow = new DataRows.DataRows();
                EmptyRow.AddField(new DataField(mapper.FieldName(),mapper.GetDefValue()));
                mapper.ToControls(EmptyRow);
            }
            
                
  
        }
        public virtual string Insert(string Table, string IdName, List<AbstractMapper> AllMappers)
        {
            List<AbstractMapper> copy = new List<AbstractMapper>();
            Mapper_IdMapper idmap = new Mapper_IdMapper(IdName,"", Table);
            Mapper_FieldStub idbr = new Mapper_FieldStub("IDBR", GlobalStates.IDBR.ToString());
            copy.Add(idmap);
            copy.Add(idbr);
            copy.AddRange(AllMappers);
            queryBuilder.Create(copy, GlobalStates.IDBR);
            queryBuilder.Execute();
            
            return ((QueryInsertBuilder)queryBuilder).GeneratedID;
        }
    }
}
